package com.epfresh.supplier.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.adapter.ViewHolder;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.FormatUtil;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.BookInfoEntity;
import com.epfresh.supplier.entity.DealDetailEntity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookDetailMonthActivity extends BaseActivity {


    private TextView tv_time_now,tv_already_money_total,tv_should_money_total;
    View tvMoreBest,tvMoreDetail,ivTime;
    ListView lvDetail;
    DetailAdapter detailAdapter;

    ArrayList<DealDetailEntity.ContentBean> detailList = new ArrayList<>();

    String timeValue;
    private String totalReceivableAmount,totalSettledAmount;//应结,已结

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_month_detail);
        timeValue = getIntent().getStringExtra("timeValue");
        ivBack.setVisibility(View.VISIBLE);
        View headView;
        headView = getLayoutInflater().inflate(R.layout.include_book_head_detail_new, null);

        tv_time_now = (TextView) headView.findViewById(R.id.tv_time_now);
        tv_should_money_total = (TextView) headView.findViewById(R.id.tv_should_money_total);//应结总金额
        tv_already_money_total = headView.findViewById(R.id.tv_already_money_total);//已结总金额
//        tv_time_now.setText(timeTitle);

        lvDetail = (ListView)findViewById(R.id.lv_deal_detail);
        lvDetail.addHeaderView(headView);
        detailAdapter = new DetailAdapter(this,detailList,R.layout.item_book_detail_new);
        lvDetail.setAdapter(detailAdapter);

//        if(getIntent().getSerializableExtra("detailList")!=null){
//            detailList.addAll((List<DealDetailEntity.ContentBean>)getIntent().getSerializableExtra("detailList"));
//            if(detailList==null||detailList.size()<=0){
//                mHttpLoad();
//            }else {
//                detailAdapter.notifyDataSetChanged();
//            }
//        }else{
//            mHttpLoad();
//        }
        mHttpLoad();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
        }
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    class DetailAdapter extends CommonAdapter<DealDetailEntity.ContentBean> implements View.OnClickListener {
        public DetailAdapter(Context context, ArrayList<DealDetailEntity.ContentBean> runPacks, int a) {
            super(context, runPacks, a);
        }

        public void onClick(View v) {
            Object tag = v.getTag(R.id.item_key_position);
            if (tag != null && tag instanceof Integer) {
                int position = (int) tag;
                    Intent detailIntent = new Intent(mContext, BookDetailDayActivity.class);
                    detailIntent.putExtra("timeTitle", detailList.get(position).getDay());
                    detailIntent.putExtra("timeValue", detailList.get(position).getDateStr());
                    startActivity(detailIntent);
            }
        }

        @Override
        public int getCount() {
            if (mDatas == null) {
                return 0;
            }
            return mDatas.size();
        }

        @Override
        public void convert(final ViewHolder holder, final DealDetailEntity.ContentBean bean) {
/*            holder.setText(R.id.tv_time, bean.getName());
            holder.setText(R.id.tv_money, bean.getPrice()+"元");*/


            holder.getView(R.id.ll_item).setTag(R.id.item_key_position, detailList.indexOf(bean));
            holder.setOnClickListener(R.id.ll_item, this);
            holder.setText(R.id.tv_time, bean.getDay());
//            holder.setText(R.id.tv_money, FormatUtil.formatDecimal(bean.getPrice()));
//            holder.setText(R.id.tv_number, bean.getSettleProgress());
//            holder.setText(R.id.tv_money_serve, bean.getSettledServiceFee());
            holder.setText(R.id.tv_money_should, bean.getReceivableAmount());
            holder.setText(R.id.tv_money_already,bean.getSettledAmount());
            if(holder.getPosition()%2==1){
                holder.getView(R.id.ll_item).setBackgroundColor(Color.WHITE);
            }else {
                holder.getView(R.id.ll_item).setBackgroundColor(Color.parseColor("#fcfcfc"));
            }



        }
    }

    /**
     * 加载收入数据
     */
    private void mHttpLoad() {
        RequestEntitySupply<Map<String, Object>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_finance_settle_income_orderTable);
        Map<String, Object> map = new HashMap<>();
        map.put("storeId", BaseApplication.getInstance().getStoreId());

        map.put("queryDate", timeValue);
        map.put("authNeedBind",BaseApplication.getInstance().getMerchant().isAuthNeedBind());
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_finance_settle_income_orderTable, onRequestListener);
    }
    private OnRequestListener<DealDetailEntity> onRequestListener = new OnRequestListener<DealDetailEntity>() {
        @Override
        public DealDetailEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, DealDetailEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<DealDetailEntity> response, Object tag) {

            hideProgressDialog();
            DealDetailEntity responseElement = response.getResponseElement();
            totalReceivableAmount = responseElement.getTotalReceivableAmount();
            totalSettledAmount = responseElement.getTotalSettledAmount();

            toolbarTitle.setText(responseElement.getQueryDateStr()+"交易明细");
            tv_time_now.setText(responseElement.getQueryDateStr());
            tv_should_money_total.setText("应结金额:" + totalReceivableAmount + "元");
            tv_already_money_total.setText("已结金额:" + totalSettledAmount + "元");

            detailList.clear();
            detailList.addAll(responseElement.getContent());
            detailAdapter.notifyDataSetChanged();

        }
    };
}
